viewOp = _dss_editor._canvas._operation.viewOp;

viewOp.fire = function(eventObj) {
	try {
		switch (eventObj.eventType) {
			case MOUSE_L_DOWN:
				return this.handleLMouseDown(eventObj);
				break;
			case MOUSE_R_DOWN:
				return this.handleRMouseDown(eventObj);
				break;
			case MOUSE_UP:
				return this.handleMouseUp(eventObj);
				break;
			case MOUSE_MOVE:
				return this.handleMouseMove(eventObj);
				break;
			case MOUSE_DBCLICK:
				return this.handleMouseDBClick(eventObj);
				break;
			case MOUSE_CLICK:
				return this.handleMouseClick(eventObj);
				break;
			case MOUSE_ONCONTEXTMENU:
				return this.handleOncontextmenu(eventObj);
				break;
			default:
				this.handleKey(eventObj);
				break;
		}
	} catch (exc) {
		console.error(exc.stack);
	}

};

viewOp.handleMouseDown = function(eventObj) {
	var canvas = eventObj.canvas;
	if (paletteViewer.selectionItem == "Select" || paletteViewer.selectionItem === '') {
		var diagram = canvas.onDiagram;
		var link = canvas.onLink;
		var hanlder = _dss_editor._toolkit.findHandleByLocation(canvas, eventObj.location);
		if (hanlder != null) {
			hanlder.handle();
		} else {
			if (!canvas.addSelection) {
				canvas.removeSelectionSystem();
				canvas.removeSelectionLink();
			}
			if (link) {
				canvas.addSelectionLink(link);
			} else if (diagram != canvas.sys) {
				canvas.addSelectionSystem(diagram);
				canvas.mousePosition = MOVE_SYSTEM;
			}
			canvas.update();
			createContextMenu(eventObj.location, canvas);
			buildMenu(eventObj.location, canvas);
		}
		return true;
	}
	return false;
	// handleCanvasOncontextmenu(eventObj,canvas);
};

viewOp.handleRMouseDown = function(eventObj) {
	return this.handleMouseDown(eventObj);
};

viewOp.handleLMouseDown = function(eventObj) {
	eventObj.canvas.mouseStatus = DRAG;
	return this.handleMouseDown(eventObj);
};

viewOp.handleMouseUp = function(eventObj) {
	var canvas = eventObj.canvas;
	for (var i = 0; i < canvas.selectionDiagrams.length; i++) {
		var diagram = canvas.selectionDiagrams[i];
		console.log(canvas.mousePosition);
		if (diagram.hasOwnProperty('newBounds')) {			
			if (canvas.mousePosition === MOVE_SYSTEM 
			    && canvas.onDiagram != diagram.parent
			    && canvas.onDiagram != diagram) {
				var command = new AddCommand(canvas,diagram);
				_dss_editor._command._commandsManager.doCommand(command);	
			}else{	
				var rectangle = _dss_editor._toolkit.translateToRelative(diagram, diagram.newBounds);
				if (diagram.type == EXITPOINT || diagram.type == PRECOND || diagram.type == POSTCOND) {
					rectangle = diagram.newBounds;
				}							
				var ccmd = new ChangeConstraintCommand(diagram, rectangle);
				_dss_editor._command._commandsManager.doCommand(ccmd);				
			}			
			_dss_editor._model.system.refreshSystemSelectionHandle(canvas);
			delete diagram.newBounds;
		}
	}
	switch (canvas.mousePosition) {
		case LINK_SRC:
			var link = canvas.selectionLinks[canvas.selectionLinks.length - 1];
			this.reconnectLinkSrc(link, canvas, eventObj);
			break;
		case LINK_TAR:
			var link = canvas.selectionLinks[canvas.selectionLinks.length - 1];
			this.reconnectLinkTar(link, canvas, eventObj);
			break;
	}
	;
	canvas.mousePosition = DEFAULT;
	canvas.mouseStatus = MOVE;
	canvas.matchSize();
	canvas.update();
	canvas.entity.style.cursor = 'default';
	return true;
};

// move ----------------------------------------
viewOp.handleMouseMove = function(eventObj) {
	var canvas = eventObj.canvas;
	// console.log(canvas.entity.style.cursor);
	canvas.onDiagram = _dss_editor._toolkit.findSystemByLocation(canvas.sys.getChildren(), eventObj.location, canvas);
	canvas.onLink = _dss_editor._toolkit.findLinkByLocation(eventObj.location, canvas);

	if (canvas.onDiagram != null) {
		this.moveOnDiagram(eventObj);
	}
	if (canvas.mouseStatus == DRAG) {
		this.mouseDrag(eventObj);
	} else if (canvas.mouseStatus == MOVE) {
		this.mouseMove(eventObj);
	}
	return true;
};

viewOp.mouseDrag = function(eventObj) {
	var canvas = eventObj.canvas;
	switch (canvas.mousePosition) {
		case RESIZE_LEFT:
		case RESIZE_RIGHT:
		case RESIZE_TOP:
		case RESIZE_BOTTOM:
		case RESIZE_L_T:
		case RESIZE_R_B:
		case RESIZE_R_T:
		case RESIZE_L_B:
			this.resizeDiagram(eventObj.location, canvas);
			break;
		case LINK_SRC:
			var link = canvas.selectionLinks[canvas.selectionLinks.length - 1];
			this.moveLinkSrc(link, eventObj.location, canvas, eventObj);
			break;
		case LINK_TAR:
			var link = canvas.selectionLinks[canvas.selectionLinks.length - 1];
			this.moveLinkTar(link, eventObj.location, canvas, eventObj);
			break;
		case MOVE_SYSTEM:
			this.moveDiagram(eventObj, canvas);
			break;
		default:
			canvas.entity.style.cursor = "default";
			break;
	}
	;
};

viewOp.mouseMove = function(eventObj) {
	var canvas = eventObj.canvas;
	var cursor = '';
	var handle = _dss_editor._toolkit.findHandleByLocation(canvas, eventObj.location);
	canvas.mousePosition = SPACE;
	if (paletteViewer.selectionItem.type != null) {
		canvas.mousePosition = this.moveWhenCreate(eventObj, canvas);
	} else if (handle != null && handle.owner.style != POINT) {
		canvas.mousePosition = handle.onSelect();
	}

	//	if()
	switch (canvas.mousePosition) {
		case RESIZE_LEFT:
			cursor = 'w-resize';
			break;
		case RESIZE_RIGHT:
			cursor = 'e-resize';
			break;
		case RESIZE_TOP:
			cursor = 'n-resize';
			break;
		case RESIZE_BOTTOM:
			cursor = 's-resize';
			break;
		case RESIZE_L_T:
			cursor = 'nw-resize';
			break;
		case RESIZE_R_B:
			cursor = 'se-resize';
			break;
		case RESIZE_R_T:
			cursor = 'ne-resize';
			break;
		case RESIZE_L_B:
			cursor = 'sw-resize';
			break;
		case LINK_SRC:
		case LINK_TAR:
			cursor = 'pointer';
			break;
		case MOUSE_CREATE:
			cursor = 'crosshair';
			break;
		case MOUSE_FORBID:
			cursor = 'no-drop';
			break;
		default:
			cursor = "default";
			break;
	};
	canvas.entity.style.cursor = cursor;
};

viewOp.reconnectLinkSrc = function(link, canvas, eventObj) {
	if (link.hasOwnProperty('newSrcPoint')) {
		var point = this.calculateAnchorPoint(canvas, link.newSrcPoint);
		if (!point) {
			canvas.links.push(link);
		} else {
			var cmd = new ReconnectLinkCommand(link, canvas);
			cmd.source = canvas.onDiagram;
			if(canvas.onLink){
				cmd.source = canvas.onLink;
			}
			cmd.target = link.getToSystem();
			cmd.sourcePoint = point;
			cmd.targetPoint = _dss_editor._model.link.getAnchorPoint(link.getToSystem(), link.showInfo.toAngle);
			// cmd.execute();
			_dss_editor._command._commandsManager.doCommand(cmd);
		}
		delete link.newSrcPoint;
	} else {
		canvas.links.push(link);
	}
};

viewOp.moveLinkSrc = function(link, location, canvas, eventObj) {
	var targetPoint = _dss_editor._model.link.getAnchorPoint(link.getToSystem(), link.showInfo.toAngle);
	var sourcePoint = location;
	link.newSrcPoint = location;
	_dss_editor._canvas._link_layer.drawLinkFeedBack(canvas, sourcePoint, targetPoint, link, eventObj);
};

viewOp.reconnectLinkTar = function(link, canvas, eventObj) {
	if (link.hasOwnProperty('newTarPoint')) {
		var point = this.calculateAnchorPoint(canvas, link.newTarPoint);
		if (!point) {
			canvas.links.push(link);
		} else {
			var cmd = new ReconnectLinkCommand(link, canvas);
			cmd.source = link.getFromSystem();
			cmd.target = canvas.onDiagram;
			cmd.sourcePoint = _dss_editor._model.link.getAnchorPoint(link.getFromSystem(), link.showInfo.fromAngle);
			cmd.targetPoint = point;
			// cmd.execute();
			_dss_editor._command._commandsManager.doCommand(cmd);
		}
		delete link.newTarPoint;
	} else {
		canvas.links.push(link);
	}
};

viewOp.moveLinkTar = function(link, location, canvas, eventObj) {
	var targetPoint = location;
	var sourcePoint = _dss_editor._model.link.getAnchorPoint(link.getFromSystem(), link.showInfo.fromAngle);
	link.newTarPoint = location;
	_dss_editor._canvas._link_layer.drawLinkFeedBack(canvas, sourcePoint, targetPoint, link, eventObj);
};

// create 时 移动的情形
viewOp.moveWhenCreate = function(eventObj, canvas) {
	if (paletteViewer.selectionItem.type == LINK) {
		if (_dss_editor._command._commandsManager.undostack[_dss_editor._command._commandsManager.undostack.length - 1] instanceof CreateLinkCommand) {
			// var cmd = cmdstack[cmdstack.length - 1];
			var cmd = _dss_editor._command._commandsManager.undostack[_dss_editor._command._commandsManager.undostack.length - 1];
			if (!cmd.isComplete) {
				_dss_editor._canvas._link_layer.drawLinkFeedBack(canvas, cmd.source, eventObj.location, cmd.link, eventObj);
			}
		}
		if (!canvas.onLink && !this.moveAroundDiagram(canvas, eventObj.location) && canvas.onDiagram.style != POINT) {
			return MOUSE_FORBID;
		}
	} else {
		canvas.update();
		var bounds = {};
		bounds.x = eventObj.location.x;
		bounds.y = eventObj.location.y;
		bounds.w = SYSTEMWIDTH;
		bounds.h = SYSTEMHEIGHT;
		if (paletteViewer.selectionItem.type == POINT) {
			bounds.w = POINTLENGTH;
			bounds.h = POINTLENGTH;
			if (canvas.onDiagram == canvas.sys) {
				return MOUSE_FORBID;
			}
			if (paletteViewer.selectionItem.name == EXITPOINT || paletteViewer.selectionItem.name == PRECOND || paletteViewer.selectionItem.name == POSTCOND) {
				var loc = this.moveAroundDiagram(canvas, eventObj.location);
				if (!loc) {
					return MOUSE_FORBID;
				} else {
					bounds = _dss_editor._toolkit.calculatePointBounds(loc, canvas.onDiagram);
				}
			}
		}

		_dss_editor._canvas._system_layer.drawFeedback(bounds, canvas);
	}
	return MOUSE_CREATE;
};

// 移动图元
viewOp.moveDiagram = function(eventObj, canvas) {
	canvas.update();
	for (var i = 0; i < canvas.selectionDiagrams.length; i++) {
		var diagram = canvas.selectionDiagrams[i];
		var bounds = _dss_editor._toolkit.translateToAbsolute(diagram);
		var feedBounds = bounds;
		var loc = eventObj.location;

		if (diagram.style == POINT) {
			if (_dss_editor._toolkit.isAroundParent(diagram)) {
				var location = this.moveAroundDiagram(canvas, eventObj.location);
				if (!location) {
					canvas.entity.style.cursor = 'no-drop';
					delete diagram.newBounds;
					continue;
				} else {
					bounds = location;
					feedBounds = _dss_editor._toolkit.calculatePointBounds(location, diagram.parent);
				}
			} else if (canvas.onDiagram == canvas.sys) {
				canvas.entity.style.cursor = 'no-drop';
				delete diagram.newBounds;
				continue;
			}
		}
		if (diagram.hasOwnProperty("offset") && !_dss_editor._toolkit.isAroundParent(diagram)) {
			loc.x -= diagram.offset.x;
			loc.y -= diagram.offset.y;
			bounds.x = loc.x;
			bounds.y = loc.y;
		}

		canvas.entity.style.cursor = 'move';
		diagram.newBounds = bounds;
		_dss_editor._canvas._system_layer.drawFeedback(feedBounds, canvas);
	}
};

viewOp.resizeDiagram = function(loc, canvas) {
	var editArea = canvas.mousePosition;
	canvas.update();
	for (var i = 0; i < canvas.selectionDiagrams.length; i++) {
		var diagram = canvas.selectionDiagrams[i];
		if (diagram.style != SYSTEM) {
			continue;
		}
		var bounds = _dss_editor._toolkit.translateToAbsolute(diagram);
		switch (editArea) {
			case RESIZE_LEFT:
				bounds.w += bounds.x - loc.x;
				bounds.x = loc.x;
				canvas.entity.style.cursor = 'w-resize';
				break;
			case RESIZE_RIGHT:
				bounds.w -= bounds.x + bounds.w - loc.x;
				canvas.entity.style.cursor = 'e-resize';
				break;
			case RESIZE_TOP:
				bounds.h += bounds.y - loc.y;
				bounds.y = loc.y;
				canvas.entity.style.cursor = 'n-resize';
				break;
			case RESIZE_BOTTOM:
				bounds.h -= bounds.y + bounds.h - loc.y;
				canvas.entity.style.cursor = 's-resize';
				break;
			case RESIZE_L_T:
				bounds.w += bounds.x - loc.x;
				bounds.h += bounds.y - loc.y;
				bounds.x = loc.x;
				bounds.y = loc.y;
				canvas.entity.style.cursor = 'nw-resize';
				break;
			case RESIZE_R_B:
				bounds.w -= bounds.x + bounds.w - loc.x;
				bounds.h -= bounds.y + bounds.h - loc.y;
				canvas.entity.style.cursor = 'se-resize';
				break;
			case RESIZE_R_T:
				bounds.w -= bounds.x + bounds.w - loc.x;
				bounds.h += bounds.y - loc.y;
				bounds.y = loc.y;
				canvas.entity.style.cursor = 'ne-resize';
				break;
			case RESIZE_L_B:
				bounds.w += bounds.x - loc.x;
				bounds.h -= bounds.y + bounds.h - loc.y;
				bounds.x = loc.x;
				canvas.entity.style.cursor = 's-resize';
				break;
			default:
				break;
		}
		diagram.newBounds = bounds;
		_dss_editor._canvas._system_layer.drawFeedback(bounds, canvas);
	}
};

viewOp.moveOnDiagram = function(eventObj) {
	var canvas = eventObj.canvas;
	var diagram = canvas.onDiagram;
	if (diagram != canvas.sys) {
		if (canvas.mousePosition != MOVE_SYSTEM) {
			var bounds = _dss_editor._toolkit.translateToAbsolute(diagram);
			diagram.offset = {
				x : eventObj.location.x - bounds.x,
				y : eventObj.location.y - bounds.y
			};
			// offset为鼠标为位置与图形之间的偏移量
		}
	}
};

viewOp.calculateAnchorPoint = function(canvas, location) {
	var diagram = canvas.onDiagram;
	var link = canvas.onLink;
	
	var point = false;
	if (link) {
		point = _dss_editor._model.link.getAnchorByLink(link);
	} else if (diagram){
	    var bounds = _dss_editor._toolkit.translateToAbsolute(diagram);
		if (diagram.style == SYSTEM) {
			point = this.moveAroundDiagram(canvas, location);
		} else if (diagram.type == BEGINPOINT) {
			point = {
				x : bounds.x + bounds.w,
				y : bounds.y + bounds.h / 2
			};
		} else if (diagram.type == ENDPOINT) {
			point = {
				x : bounds.x,
				y : bounds.y + bounds.h / 2
			};
		}
	}
	return point;
};

/**
 * 判断创建link的时候，鼠标在图元的位置，
 * 如果在图元的边界上，那么返回 在周围的这个点，表示可以在这里创建link 否则返回false，表示不能创建
 */
viewOp.moveAroundDiagram = function(canvas, location) {
	var diagram = canvas.onDiagram;
	var bounds = {};
	var point = location;
	var volume = 12;

	if (diagram.style != SYSTEM || diagram == canvas.sys) {
		return false;
	}
	bounds = _dss_editor._toolkit.translateToAbsolute(diagram);
	if (diagram.isShowLabel() && diagram.isShowContent()) {
		bounds.y += labelHeight;
		bounds.h -= labelHeight;
	}
	if (point.x - bounds.x >= 0 && point.x - bounds.x <= volume) {
		point.x = bounds.x;
		return point;
	} else if (bounds.x + bounds.w - point.x >= 0 && bounds.x + bounds.w - point.x <= volume) {
		point.x = bounds.x + bounds.w;
		return point;
	} else if (point.y - bounds.y >= 0 && point.y - bounds.y <= volume) {
		point.y = bounds.y;
		return point;
	} else if (bounds.y + bounds.h - point.y >= 0 && bounds.y + bounds.h - point.y <= volume) {
		point.y = bounds.y + bounds.h;
		return point;
	}
	return false;
};

// ------------------------
viewOp.handleMouseDBClick = function(eventObj, canvas) {
};

viewOp.handleMouseClick = function(eventObj) {
	var canvas = eventObj.canvas;
	if (paletteViewer.selectionItem.type) {
		if (paletteViewer.selectionItem.type == SYSTEM || paletteViewer.selectionItem.type == POINT) {
			var location = eventObj.location;
			if (paletteViewer.selectionItem.name == EXITPOINT || paletteViewer.selectionItem.name == PRECOND || paletteViewer.selectionItem.name == POSTCOND) {
				location = this.moveAroundDiagram(canvas, eventObj.location);
			}
			this.createSystem(location, canvas);
		} else if (paletteViewer.selectionItem.type == LINK) {
			this.createLink(eventObj, canvas);
		}
	}	
};

viewOp.createSystem = function(location, canvas) {
	var type = null;
	//paletteViewer.selectionItem.name;
	var style = null;
	//paletteViewer.selectionItem.type;
	var baseOn = null;
	if (paletteViewer.selectionItem.hasOwnProperty('baseOn')) {
		baseOn = paletteViewer.selectionItem.baseOn;
		//这里以后如果出现不是baseOn一个基本类型sys的时候需要增加一些判断
	}
	if (paletteViewer.selectionItem.hasOwnProperty('name')) {
		type = paletteViewer.selectionItem.name;
	}
	if (paletteViewer.selectionItem.hasOwnProperty('type')) {
		style = paletteViewer.selectionItem.type;
	}
	var t = {
		type : type,
		style : style,
		baseOn : baseOn
	};
	//	var location = eventObj.location;
	var parent = canvas.onDiagram;
	var command = new CreateCommand(t, location, parent);
	_dss_editor._command._commandsManager.doCommand(command);
	var system = _dss_editor._command._commandsManager.undostack[_dss_editor._command._commandsManager.undostack.length-1].system;
	canvas.removeSelectionSystem();
	canvas.addSelectionSystem(system);
	canvas.update();
	restoreAllFolder(canvas);
};

viewOp.createLink = function(eventObj, canvas) {
	var point = this.calculateAnchorPoint(canvas, eventObj.location);
	var type = paletteViewer.selectionItem.name;
	var command = _dss_editor._command._commandsManager.undostack[_dss_editor._command._commandsManager.undostack.length - 1];
	if(command instanceof CreateLinkCommand && command.isFirst && !command.isNext) {		
		command.pointTo = point;
		_dss_editor._command._commandsManager.doCommand(command);
	} else {
		command = new CreateLinkCommand(type, canvas);
		if(canvas.onDiagram.type == BEGINPOINT) {
			var bounds = _dss_editor._toolkit.translateToAbsolute(canvas.onDiagram);
			point = {
				x : bounds.x + bounds.w,
				y : bounds.y + bounds.h /2
			};
		}
		if(!point) {
			return;
		}
		command.pointFrom = point;
		command.canvas = canvas;
		_dss_editor._command._commandsManager.doCommand(command);
	}
};

viewOp.handleOncontextmenu = function(eventObj) {
	var canvas = eventObj.canvas;
	if (paletteViewer.selectionItem != "Select") {
		restoreAllFolder(canvas);
		canvas.entity.style.cursor = 'default';
	}
};
viewOp.handleKey = function(eventObj) {
	var canvas = eventObj.canvas;
	if (eventObj.ctrl) {
		switch (eventObj.eventType) {
			case KEY_C:
				eventObj.eventType = CMD_COPY;
				_dss_editor.fireCommandOp(eventObj);
				break;
			case KEY_V:
				eventObj.eventType = CMD_PASTE;
				_dss_editor.fireCommandOp(eventObj);
				break;
			case KEY_D:
				for (var i in canvas.selectionDiagrams) {
					var diagram = canvas.selectionDiagrams[i];
					if (diagram.hasShadow()) {
						eventObj.eventType = CMD_REMOVE_SHADOW;
					} else {
						eventObj.eventType = CMD_ADD_SHADOW;
					}
					_dss_editor.fireCommandOp(eventObj);
				}
				break;
			case KEY_O:
				for (var i in canvas.selectionDiagrams) {
					var diagram = canvas.selectionDiagrams[i];
					if (diagram.isShowContent()) {
						eventObj.eventType = CMD_HIDE_CONTENT;
					} else {
						eventObj.eventType = CMD_SHOW_CONTENT;
					}
					_dss_editor.fireCommandOp(eventObj);
				}
				break;
			case KEY_L:
				for (var i in canvas.selectionDiagrams) {
					var diagram = canvas.selectionDiagrams[i];
					if (diagram.isShowLabel()) {
						eventObj.eventType = CMD_HIDE_NAME;
					} else {
						eventObj.eventType = CMD_SHOW_NAME;
					}
					_dss_editor.fireCommandOp(eventObj);
				}
				break;
			case KEY_Z:
				if(_dss_editor._command._commandsManager.undostack.length > 0) {					
					_dss_editor._command._commandsManager.undoCommand(eventObj);
				} else {
					alert("没有可以撤销的内容!");
				}
				break;
			case KEY_R:
				if(_dss_editor._command._commandsManager.redostack.length > 0) {
					_dss_editor._command._commandsManager.redoCommand(eventObj);
				} else {
					alert("没有可以重做的内容!");
				}
				break;
			default:
				break;
		}
	} else {
		switch (eventObj.eventType) {
			case KEY_ESC:
				restoreAllFolder(eventObj.canvas);
				break;
			case KEY_F1:
				this.navigate(eventObj.canvas);
				break;
			case KEY_DELETE:
				eventObj.eventType = CMD_DELETE;
				_dss_editor.fireCommandOp(eventObj);
				break;
			default:
				break;
		}
	}
};

//以后需要考虑namelabel的情况
viewOp.navigate = function(canvas) {
	var selection = canvas.selectionDiagrams[0];
	var name;
	var target;
	var domain = selection.parent;
	if (!selection || selection.style === STYLE_ROOT) {
		selection = canvas.selectionLinks[0];
	}
	if (selection.style === SYSTEM) {
		name = selection.getValue();
	} else {
		name = selection.content;
		domain = selection.fromSystem.parent;
	}
	if (name) {
		target = _dss_editor._namespace.findSysInExNS(name, domain);		
	}
	if (target) {
		_dss_editor._toolkit.gotoSystem(target, canvas);
	}
};
delete viewOp;
